<?php


namespace App\Http\Services\MeiTuan;


use App\MeituanConfig;

class Config
{
    //配送开放平台
    protected $appKey   = '9c2b47d708fa48b4b1516ee51af6daca';//9c2b47d708fa48b4b1516ee51af6daca是测试的key
    protected $secret   = 'xd#N3|ydANDHp]Sy4X8v=I^((^)(|SQhZhU8hv<g`ZW62)1?aGnV5Bdvr<otPN3?';
    protected $version  = '1.0';//不用改

    //服务商
    protected $s_developerId    = 106383;   //自定义，Long,服务商身份标识,106383测试号
    protected $s_signKey                = 'ysqyufulxn5bqc3h';//自定义
    protected $s_charset        = 'utf-8';   //不用改
    protected $s_version        = '2';      //不用改
    protected $s_business_id    = 19;       //不用改:收银-茶饮版17/收银-智能版18/配送19


    public function getSystemPayload(): array
    {
        return [
            'appkey'=>$this->appKey,
            'timestamp'=>time(),
            'version'=>$this->version
        ];
    }
    public function getSignSecret($appkey = null): string
    {
        if (!$appkey)
            return $this->secret;
        if ($this->appKey == $appkey)
            return $this->secret;
    }
    public function verifySign(array $payload): bool
    {
        if (!isset($payload['appkey']))
            return false;
        if (!isset($payload['sign']))
            return false;
        $sort = ksort($payload);
        $str = '';
        if (!$sort)
            return false;
        foreach ($payload as $key => $value) {
            if ($key != '' && $key != 'sign' && $value != '')
                $str = $str . $key . $value;
        }
        $str = $this->getSignSecret($payload['appkey']) . $str;
        $sign = bin2hex(sha1($str, true));
        if ($sign == $payload['sign'])
            return true;
        return false;
    }

    //服务商
    public function getServiceProviderPublicPayload(): array
    {
        return [
//            'appAuthToken'  =>'',
            'timestamp'     =>time(),
            'charset'       =>$this->s_charset,
            'developerId'   =>$this->s_developerId,
            'version'       =>$this->s_version,
            'businessId'    =>(int)$this->s_business_id
        ];
    }
    public function getServiceProviderSignKey($developerId = 0): string
    {
        if (!$developerId)
            return $this->s_signKey;
        if ($developerId == $this->s_developerId){
            return $this->s_signKey;
        }
    }


}
